package com.acme.bankapp.domain;

import com.acme.bankapp.domain.accounts.Account;
import com.acme.bankapp.exception.NotEnoughFundsException;

import java.io.Serializable;

/**
 * Created by Student on 14.11.2014.
 */
public class Client implements Serializable {
    private Account account;
    private String name;
    private Gender sex;

    public Client(String name, Gender sex) {
        this.name = name;
        this.sex = sex;
    }

    public Client(String name, Gender sex, Account account) {
        this.name = name;
        this.sex = sex;
        this.account = account;
    }

    public Gender getSex() {
        return sex;
    }

    public void setSex(Gender sex) {
        this.sex = sex;
    }

    public Account getAccount() {
        return account;
    }

    public boolean setAccount(Account account) {
        if (this.account == null) {
            this.account = account;
            return true;
        }
        else
            return false;
    }

    public void changeBalance(double amount) throws NotEnoughFundsException {

        if( amount < 0 ){
            account.withdraw(amount * (-1));
        } else {
            account.deposit(amount);
        }
    }

    public String getName() {
        return name != null ? name : "";
    }

    public String getClientSalutation() {
        return sex.getSalutation() + " " + name;
    }

    public void printInfo() {
        System.out.println("Balance: " + account.getBalance());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Client{").append("Name='").append(name).append("', Sex=").append(sex).append(", Account=").append(account).append("}");
        return sb.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Client client = (Client) o;

        if (account != null ? !account.equals(client.account) : client.account != null) return false;
        if (name != null ? !name.equals(client.name) : client.name != null) return false;
        if (sex != client.sex) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = account != null ? account.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (sex != null ? sex.hashCode() : 0);
        return result;
    }
}
